﻿#include <iostream>
#include <vector>
#include <chrono>
#include <omp.h>

int main() {
    const int N = 10000000000; // 1 亿个元素
    std::vector<int> data(N, 1); // 初始化所有元素为 1

    // --- 串行求和 ---
    auto start_serial = std::chrono::high_resolution_clock::now();
    long long sum_serial = 0;
    for (int i = 0; i < N; ++i) {
        sum_serial += data[i];
    }
    auto end_serial = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> duration_serial = end_serial - start_serial;

    // --- 并行求和（OpenMP） ---
    auto start_parallel = std::chrono::high_resolution_clock::now();
    long long sum_parallel = 0;

#pragma omp parallel for reduction(+:sum_parallel)
    for (int i = 0; i < N; ++i) {
        sum_parallel += data[i];
    }
    auto end_parallel = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> duration_parallel = end_parallel - start_parallel;

    // --- 输出结果 ---
    std::cout << "chuan-result: " << sum_serial << "   times: " << duration_serial.count() << " s\n";
    std::cout << "bing-result: " << sum_parallel << "  times: " << duration_parallel.count() << " s\n";

    return 0;
}
